﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Linq;
using UIT.Silverlight.Renderers;

namespace UIT.Silverlight
{
	public class Application : System.Windows.Application
	{
		internal UIT.Application application;
		public Application()
		{
			this.Startup += new StartupEventHandler(Application_Startup);
			this.Exit += new EventHandler(Application_Exit);
		}

		void Application_Exit(object sender, EventArgs e)
		{
			if (application != null)
				application.OnStop();
		}

		void Application_Startup(object sender, StartupEventArgs e)
		{
			UIT.Context.GetContext = () => new Context();

			if(ApplicationType==null)
				ApplicationType = Assembly.GetExecutingAssembly().GetExportedTypes().FirstOrDefault(t => t.BaseType == typeof(UIT.Application));
			if (ApplicationType != null)
			{
				application = (UIT.Application)Activator.CreateInstance(ApplicationType);
				application.OnStart();
			}
			Uri uri = StartupUri;
			SlRendererManager.EnsureInitialized();
			RootVisual = SlRendererManager.Render(uri);
		}

		public Uri StartupUri { get; set; }

		public Type ApplicationType { get; set; }
	}
}
